const cronJob = require("cron").CronJob
const cron = new cronJob(
	"0 0 3 * * *",
	() => {
		rmBin()
	},
	null,
	false,
	"Asia/Chongqing"
)
async function rmBin() {
	const fileModel = require("../model/File")
	const fs = require("fs")
	const path = require("path")
	const { Op } = require("sequelize");
	// 获取当前静态文件夹下所有文件夹
	const realFiles = fs.readdirSync(path.join(__dirname + "../../../public/"))
	// 排除api文档文件夹
	realFiles.splice(realFiles.indexOf("doc"), 1)
	
	// 获取文件夹内所有文件
	var realFileArr = []
	for (let item of realFiles) {
		var temp = fs.readdirSync(path.join(__dirname + "../../../public/" + item))
		temp = temp.forEach(async name => {
			let filePath = "/" + item + "/" + name
			const fileData = await fileModel.findAll({
				where:{
					content:{
						[Op.like]:'%'+filePath+'%'
					}
				}
			})
			if(!fileData.length){
				fs.unlinkSync(path.join(__dirname + "../../../public/" + filePath))
			}
		})
		
	}
	

	
}

module.exports = cron
